package com.zhangsun.controller;

import com.zhangsun.bean.QuartzBean;
import com.zhangsun.bean.ResponseResult;
import com.zhangsun.service.QuartzJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/quartz")
public class QuartzController {

    @Autowired
    private QuartzJobService quartzJobService;

    /**
     * 获取所有任务
     *
     * @return 任务
     */
    @GetMapping("/findAll")
    public ResponseResult findAll() {
        List<QuartzBean> jobs = quartzJobService.findAll();
        return ResponseResult.success("获取成功", jobs);
    }

    /**
     * 添加任务
     *
     * @param jobClassName   class名
     * @param jobGroupName   组名
     * @param cronExpression 表达式
     */
    @PostMapping("/create")
    public ResponseResult create(String jobClassName, String jobGroupName, String cronExpression) {
        quartzJobService.create(jobClassName, jobGroupName, cronExpression);
        return ResponseResult.success("添加成功");
    }

    /**
     * 更新任务
     *
     * @param jobClassName   class名
     * @param jobGroupName   组名
     * @param cronExpression 表达式
     */
    @PostMapping("/update")
    public ResponseResult update(String jobClassName, String jobGroupName, String cronExpression) {
        quartzJobService.update(jobClassName, jobGroupName, cronExpression);
        return ResponseResult.success("更新成功");
    }

    /**
     * 暂停任务
     *
     * @param jobClassName class名
     * @param jobGroupName 组名
     */
    @PostMapping("/pause")
    public ResponseResult pause(String jobClassName, String jobGroupName) {
        quartzJobService.pause(jobClassName, jobGroupName);
        return ResponseResult.success("暂停成功");
    }

    /**
     * 重新开始任务
     *
     * @param jobClassName class名
     * @param jobGroupName 组名
     */
    @PostMapping("/resume")
    public ResponseResult resume(String jobClassName, String jobGroupName) {
        quartzJobService.resume(jobClassName, jobGroupName);
        return ResponseResult.success("重新开始成功");
    }

    /**
     * 删除任务
     *
     * @param jobClassName class名
     * @param jobGroupName 组名
     */
    @PostMapping("/delete")
    public ResponseResult delete(String jobClassName, String jobGroupName) {
        quartzJobService.delete(jobClassName, jobGroupName);
        return ResponseResult.success("删除成功");
    }

}
